﻿import QtQuick 2.3
import QtQuick.Controls 1.2
import QmlObjectTest 1.0
import QtQuick.Dialogs 1.2
import QtQuick.Window 2.2
import QtQuick.Controls.Styles 1.4
import QtQuick.Controls.Private 1.0
import QtMultimedia 5.0
import MyCommon 1.0

ApplicationWindow {
    id: window
    visible: true
    width: Screen.width
    height: Screen.height

    property real startX: 0

    MyCommon{
        id: myCommon
    }

    Item{
        focus: true
        Keys.onPressed: {
            if(event.key == Qt.Key_Back){
                messageDialogExit.visible = true
                event.accepted = true
            }
        }
    }    

    Camera{
        id: camera
        objectName: "my_camera"
        captureMode: Camera.CaptureStillImage
        imageCapture {
            onImageCaptured: {
                listModel.append({"sor": preview})
//                photoPreview.source = preview  // Show the preview in an Image
            }
            onImageSaved: {
                console.log("path", path)
                myCommon.imageSave(path)
            }
//            resolution: Qt.size(Screen.width, Screen.height)
        }

        focus {
            id: cameraFocus
            focusMode: Camera.FocusAuto | Camera.FocusMacro | Camera.FocusContinuous | Camera.FocusManual
            focusPointMode: Camera.FocusPointFaceDetection
            customFocusPoint: Qt.point(0.2, 0.2) // Focus relative to top-left corner
        }

    }

    VideoOutput {
        source: camera
        orientation: -90
        width: window.width
        height: window.height
        fillMode: VideoOutput.Stretch
        focus : visible // to receive focus and capture key events when visible
    }

    MouseArea{
        anchors.fill: parent
        onClicked: {
            cameraFocus.customFocusPoint = Qt.point(0.2, 0.2)
            camera.imageCapture.capture()
        }
    }


    Rectangle{
        id: rectDis
        width: 0
        height: 0
        color: "white"
        visible: !(sor=="")
        z: 1
        clip: true

        property string sor: ""

        Image{
            id: imgDis
            anchors.centerIn: parent
            width: parent.width - 10
            height: parent.height - 10
            source: rectDis.sor
            fillMode: Image.Stretch
        }

        MouseArea{
            anchors.fill: parent
            enabled: rectDis.visible
            onClicked: {
                animClose.restart()
            }
        }
    }

    ParallelAnimation{
        id: animOpen
        PropertyAnimation{target: rectDis; property: "x"; from: startX; to: 0; duration: 200}
        PropertyAnimation{target: rectDis; property: "y"; from: Screen.height-100; to: 0; duration: 200}

        PropertyAnimation{target: rectDis; property: "width"; from: 0; to: Screen.width; duration: 200}
        PropertyAnimation{target: rectDis; property: "height"; from: 0; to: Screen.height; duration: 200}
    }

    ParallelAnimation{
        id: animClose
        PropertyAnimation{target: rectDis; property: "x"; from: 0; to: startX; duration: 200}
        PropertyAnimation{target: rectDis; property: "y"; from: 0; to: Screen.height-100; duration: 200}

        PropertyAnimation{target: rectDis; property: "width"; from: Screen.width; to: 0; duration: 200}
        PropertyAnimation{target: rectDis; property: "height"; from: Screen.height; to: 0; duration: 200}

        onStopped: {
            rectDis.sor = ""
        }
    }

    Rectangle{
        id: rectBottom
        anchors.bottom: parent.bottom
        width: parent.width
        height: 200
        color: "black"

        ListView{
            id: listView
            width: parent.width
            height: 200
            spacing: 5
            orientation: ListView.Horizontal
            model: ListModel{
                id: listModel
            }
            delegate: Image {
                id: photoPreview
                width: 200
                height: 200
                fillMode: Image.Stretch
                source: sor

                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        startX = index * photoPreview.width + listView.spacing * (index-1) + photoPreview.width/2 - listView.contentX
                        rectDis.sor = photoPreview.source
                        animOpen.restart()
                    }
                }
            }
        }
    }


//    Button{
//        text: "拍照"
//        anchors.right: parent.right
//        style: ButtonStyle{}
//        onClicked: {
////            messageDialog.visible = true
//            camera.imageCapture.capture()
//        }
//    }

    MessageDialog{
        id: messageDialog
        title: "操作方式"
        informativeText: "点击头部，手部，身体！"
        onAccepted: {
        }
    }

    MessageDialog{
        id: messageDialogExit
        title: "退出"
        informativeText: "你要退出程序吗？"
        standardButtons: StandardButton.Ok | StandardButton.Cancel

        onAccepted: {
            Qt.quit()
        }
        onRejected: {

        }
    }
}

